package com.zucai365.football.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.wcj.common.service.impl.WcjServiceImpl;
import com.zucai365.football.bean.ShouFaBean;
import com.zucai365.football.bean.TeamDzErrorLogBean;
import com.zucai365.football.dao.TeamDzErrorLogDao;
import com.zucai365.football.service.TeamDzErrorLogService;

@Service("teamDzErrorLogService")
public class TeamDzErrorLogServiceImpl extends WcjServiceImpl<TeamDzErrorLogBean> implements TeamDzErrorLogService{
	
	@Autowired
	@Qualifier("teamDzErrorLogDao")
	private TeamDzErrorLogDao teamDzErrorLogDao;

	public TeamDzErrorLogDao getTeamDzErrorLogDao() {
		return teamDzErrorLogDao;
	}

	public void setTeamDzErrorLogDao( TeamDzErrorLogDao teamDzErrorLogDao ) {
		this.teamDzErrorLogDao = teamDzErrorLogDao;
	}

	@Override
	public void saveTeamDzErrorLog( List<TeamDzErrorLogBean> teamDzErrorLogList ) {
		
		List<TeamDzErrorLogBean> logList = new ArrayList<TeamDzErrorLogBean>();
		
		for( TeamDzErrorLogBean teamDzErrorLog : teamDzErrorLogList ){
			TeamDzErrorLogBean log = getTeamDzErrorLog(teamDzErrorLog);
			if( log == null ){
				logList.add( teamDzErrorLog );
			}
		}
		this.teamDzErrorLogDao.saveAll( logList );
	}

	@Override
	public TeamDzErrorLogBean getTeamDzErrorLog( TeamDzErrorLogBean teamDzErrorLog ) {

		DetachedCriteria criteria = DetachedCriteria.forClass( TeamDzErrorLogBean.class, "teamDzErrorLog" );
		criteria.createAlias( "teamDzErrorLog.teamDz", "teamDz" );
	
		
		criteria.add( Restrictions.eq( "teamDzErrorLog.liansaiYearId", teamDzErrorLog.getLiansaiYearId() ) );
		criteria.add( Restrictions.eq( "teamDzErrorLog.liansaiId", teamDzErrorLog.getLiansaiId() ) );
		criteria.add( Restrictions.eq( "teamDz.id", teamDzErrorLog.getTeamDz().getId() ) );
		criteria.add( Restrictions.eq( "teamDzErrorLog.lunci", teamDzErrorLog.getLunci() ) );
		criteria.add( Restrictions.eq( "teamDzErrorLog.event", teamDzErrorLog.getEvent() ) );
		criteria.add( Restrictions.eq( "teamDzErrorLog.eventTime", teamDzErrorLog.getEventTime() ) );
		criteria.add( Restrictions.eq( "teamDzErrorLog.jerseyNumber", teamDzErrorLog.getJerseyNumber() ) );
		
		List<TeamDzErrorLogBean> list = this.teamDzErrorLogDao.findByCriteria(criteria);
		
		if( list.isEmpty() ){
			return null;
		}
		
		return list.get(0);
		
	}

	@Override
	public List<TeamDzErrorLogBean> searchTeamDzErrorLog(Long teamDzId) {

		DetachedCriteria criteria = DetachedCriteria.forClass( TeamDzErrorLogBean.class, "teamDzErrorLog" );
		criteria.createAlias( "teamDzErrorLog.teamDz", "teamDz" );
		
		criteria.add( Restrictions.eq( "teamDz.id", teamDzId ) );
		criteria.addOrder( Order.asc( "teamDzErrorLog.eventTime" ) );
		
		return this.teamDzErrorLogDao.findByCriteria(criteria);
		
	}
	
}
